package com.netty.cn.rpc.selfmessage.core;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

/**
 * 
 * @author wangzonghui
 * @date 2023-02-24 16:33:41
 * @Description 解码类
 */
public class MyMessageDecoder extends ByteToMessageDecoder {
    int length=0;
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        //将二进制字节码转为对象
        if(byteBuf.readableBytes()>=4){
            if(length==0){
                length=byteBuf.readInt();
            }
            if(byteBuf.readableBytes()<length){
                System.out.println("当前可读数据不够，继续等待");
                return;
            }
            byte[] content=new byte[length];
            byteBuf.readBytes(content);
            MyMessage message=new MyMessage();
            message.setLen(length);
            message.setContent(content);
            list.add(message);//传递给下一个handler
            length=0;
        }
    }

}
